from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth import login, logout, authenticate
from django.contrib.auth.decorators import login_required, user_passes_test
from .models import Book, Author, BookInstance
from .forms import AuthorForm

# Create your views here.

def index(request):
    # 获取或初始化会话中的访问计数
    num_visits = request.session.get('num_visits', 0)
    request.session['num_visits'] = num_visits + 1
    return render(request, 'catalog/index.html', {'num_visits': num_visits})

def book_list(request):
    books = Book.objects.all()
    return render(request, 'catalog/book_list.html', {'books': books})

def author_list(request):
    authors = Author.objects.all()
    return render(request, 'catalog/author_list.html', {'authors': authors})

def author_detail(request, pk):
    author = get_object_or_404(Author, pk=pk)
    return render(request, 'catalog/author_detail.html', {'author': author})

def bookinstance_list(request):
    bookinstances = BookInstance.objects.all()
    return render(request, 'catalog/bookinstance_list.html', {'bookinstances': bookinstances})

def login_view(request):
    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            return redirect('index')
    return render(request, 'catalog/login.html')

def logout_view(request):
    logout(request)
    return redirect('index')

@login_required
def my_books(request):
    bookinstances = BookInstance.objects.filter(borrower=request.user)
    return render(request, 'catalog/my_books.html', {'bookinstances': bookinstances})

@login_required
def renew_book(request, pk):
    bookinstance = get_object_or_404(BookInstance, pk=pk)
    if request.method == 'POST':
        bookinstance.due_back = request.POST.get('due_back')
        bookinstance.save()
        return redirect('my-books')
    return render(request, 'catalog/renew_book.html', {'bookinstance': bookinstance})

def author_create(request):
    if request.method == 'POST':
        form = AuthorForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('author-list')
    else:
        form = AuthorForm()
    return render(request, 'catalog/author_form.html', {'form': form})

def author_update(request, pk):
    author = get_object_or_404(Author, pk=pk)
    if request.method == 'POST':
        form = AuthorForm(request.POST, instance=author)
        if form.is_valid():
            form.save()
            return redirect('author-list')
    else:
        form = AuthorForm(instance=author)
    return render(request, 'catalog/author_form.html', {'form': form})

def author_delete(request, pk):
    author = get_object_or_404(Author, pk=pk)
    if request.method == 'POST':
        author.delete()
        return redirect('author-list')
    return render(request, 'catalog/author_confirm_delete.html', {'author': author})

@login_required
@user_passes_test(lambda u: u.is_staff)
def librarian_view(request):
    bookinstances = BookInstance.objects.filter(status='o')
    return render(request, 'catalog/librarian_view.html', {'bookinstances': bookinstances})
